﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using DataAccess;
using com.chttl.ienc.bean;
using java.util;

namespace iEN.Organization
{
    public partial class DeviceManage : System.Web.UI.Page
    {
        #region protected void Page_Load(object sender, EventArgs e) //初始頁面
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)//先判斷是否已經登入過
            {
                if (Session["LoginName"] == null)
                {
                    Response.Write("<script>alert('請先登入！！');location.href='../Login.aspx';</script>");
                }
                this.Intialize();
            }

            if (Session["SysAdmin"].ToString() != "2")
            {
                Panel1.Visible = false;
                Panel2.Visible = false;
                lblMsg.Visible = true;
                btReturn.Visible = false;
                btAddDevice.Visible = false;
            }
        }
        #endregion

        #region Initialization methods
        private void Intialize()
        {
            ddlControl.Items.Clear();
            ddlBuilding.Items.Clear();
            ddlFloor.Items.Clear();
            ddlBlock.Items.Clear();
            ddBuil.Items.Clear();
            ddfloor.Items.Clear();
            ddBlock.Items.Clear();
            //加一個空白的選項於最上方
            ListItem empty = ddlControl.Items.FindByText(string.Empty);
            if (empty == null)
            {
                ddlControl.Items.Add(new ListItem(string.Empty));
                ddlBuilding.Items.Add(new ListItem(string.Empty));
                ddlFloor.Items.Add(new ListItem(string.Empty));
                ddlBlock.Items.Add(new ListItem(string.Empty));
                ddBuil.Items.Add(new ListItem(string.Empty));
                ddfloor.Items.Add(new ListItem(string.Empty));
                ddBlock.Items.Add(new ListItem(string.Empty));
            }

            DataTable dttmp = new DBAccess().getControllerTable("1");  //取得ALLControl
            if (dttmp == null)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('資料庫抓取資料有誤！')", true);
                return;
            }
            else
            {
                for (int i = 0; i < dttmp.Rows.Count; i++)
                    this.ddlControl.Items.Add(new ListItem(dttmp.Rows[i]["_name"].ToString(),dttmp.Rows[i]["_id"].ToString()));

                DataTable dtTemp = new DBAccess().getDeviceTable("1");  //取得ALLDevice
                Session["dtDataSourceTemp"] = dtTemp;

            }            
        }
        #endregion

        #region protected void btReturn_Click(object sender, EventArgs e) //回到控制器管理頁面
        protected void btReturn_Click(object sender, EventArgs e)
        {
            this.Page.Response.Redirect("../Organization/ControllerManage.aspx");
        }
        #endregion

        #region //分頁設定
        public void pageSet(object gvQuery)
        {
            GridView gv = (GridView)gvQuery;
            if (gv.PageCount > 1)
            {
                if (gv.BottomPagerRow == null)
                    return;
                Label lbCount = (Label)gv.BottomPagerRow.FindControl("lbAllCount");
                Label lbPage = (Label)gv.BottomPagerRow.FindControl("lbAllPage");
                DropDownList ddlJump = (DropDownList)gv.BottomPagerRow.FindControl("dd1PageJump");
                //顯示資料總筆數與總頁數
                lbCount.Text = "共" + (gv.PageSize * gv.PageCount).ToString() + "筆";
                lbPage.Text = "共" + gv.PageCount + "頁";
                //設定下拉選單跳頁之頁數        
                for (int i = 1; i <= gv.PageCount; i++)
                    ddlJump.Items.Add(new ListItem(i.ToString()));
                //first:firstc和previous按鈕無作用;  last: next和last按鈕無作用  
                if (gv.PageIndex == 0)
                {
                    ((Button)gv.BottomPagerRow.FindControl("First")).Enabled = false;
                    ((Button)gv.BottomPagerRow.FindControl("Previous")).Enabled = false;
                }
                else if (gv.PageIndex == (gv.PageCount - 1))
                {
                    ((Button)gv.BottomPagerRow.FindControl("Next")).Enabled = false;
                    ((Button)gv.BottomPagerRow.FindControl("Last")).Enabled = false;
                }
                //顯示目前頁數
                ddlJump.SelectedIndex = gv.PageIndex;
            }
        }

        protected void First_Click(object sender, EventArgs e)
        {
            int intPageIndex = 0;
            Button lkbtn = (Button)sender;
            switch (lkbtn.CommandArgument.ToString())
            {
                case "Previous":
                    if (this.gvGrpName.PageIndex > 0)
                        intPageIndex = this.gvGrpName.PageIndex - 1;
                    break;
                case "Next":
                    if (this.gvGrpName.PageIndex < this.gvGrpName.PageCount - 1)
                        intPageIndex = this.gvGrpName.PageIndex + 1;
                    break;
                case "Last":
                    intPageIndex = this.gvGrpName.PageCount - 1;
                    break;
            }
            this.gvGrpName.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvGrpName.PageIndex = intPageIndex;
            this.gvGrpName.DataBind();
        }

        protected void Previous_Click(object sender, EventArgs e)
        {
            int intPageIndex = 0;
            Button lkbtn = (Button)sender;
            switch (lkbtn.CommandArgument.ToString())
            {
                case "Previous":
                    if (this.gvGrpName.PageIndex > 0)
                        intPageIndex = gvGrpName.PageIndex - 1;
                    break;
                case "Next":
                    if (this.gvGrpName.PageIndex < this.gvGrpName.PageCount - 1)
                        intPageIndex = this.gvGrpName.PageIndex + 1;
                    break;
                case "Last":
                    intPageIndex = gvGrpName.PageCount - 1;
                    break;
            }
            this.gvGrpName.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvGrpName.PageIndex = intPageIndex;
            this.gvGrpName.DataBind();
        }

        protected void Next_Click(object sender, EventArgs e)
        {
            int intPageIndex = 0;
            Button lkbtn = (Button)sender;
            switch (lkbtn.CommandArgument.ToString())
            {
                case "Previous":
                    if (this.gvGrpName.PageIndex > 0)
                        intPageIndex = this.gvGrpName.PageIndex - 1;
                    break;
                case "Next":
                    if (this.gvGrpName.PageIndex < this.gvGrpName.PageCount - 1)
                        intPageIndex = gvGrpName.PageIndex + 1;
                    break;
                case "Last":
                    if (this.gvGrpName.PageIndex != this.gvGrpName.PageCount)
                        intPageIndex = this.gvGrpName.PageCount - 1;
                    break;
            }
            this.gvGrpName.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvGrpName.PageIndex = intPageIndex;
            this.gvGrpName.DataBind();
        }

        protected void Last_Click(object sender, EventArgs e)
        {
            int intPageIndex = 0;
            Button lkbtn = (Button)sender;
            switch (lkbtn.CommandArgument.ToString())
            {
                case "Previous":
                    if (this.gvGrpName.PageIndex > 0)
                        intPageIndex = this.gvGrpName.PageIndex - 1;
                    break;
                case "Next":
                    if (this.gvGrpName.PageIndex < this.gvGrpName.PageCount - 1)
                        intPageIndex = this.gvGrpName.PageIndex + 1;
                    break;
                case "Last":
                    intPageIndex = this.gvGrpName.PageCount - 1;
                    break;
            }
            this.gvGrpName.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvGrpName.PageIndex = intPageIndex;
            this.gvGrpName.DataBind();
        }

        protected void dd1PageJump_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlpage = ((DropDownList)this.gvGrpName.BottomPagerRow.FindControl("dd1PageJump"));
            this.gvGrpName.DataSource = (DataTable)Session["dtDataSourceTemp"];
            this.gvGrpName.PageIndex = ddlpage.SelectedIndex;
            this.gvGrpName.DataBind();
        }
        #endregion

        protected void gvGrpName_DataBound(object sender, EventArgs e)
        {
            pageSet(this.gvGrpName);

        }

        #region protected void btAddDevice_Click(object sender, EventArgs e) //新增設備
        protected void btAddDevice_Click(object sender, EventArgs e)
        {
            this.ddlBuilding.Items.Clear();
            this.ddlBuilding.Items.Add(new ListItem(string.Empty));
            this.UpdatePanel3.Visible = true;
            DataTable Building = new DBAccess().getOrgGroupTable("1");  //取得ALLOrgGroup
            if (Building == null)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('資料庫抓取資料有誤！')", true);
                return;
            }
            else
            {
                Building.DefaultView.RowFilter = "_level=1";
                Building = Building.DefaultView.ToTable();
                for (int i = 0; i < Building.Rows.Count; i++)
                    this.ddlBuilding.Items.Add(new ListItem(Building.Rows[i]["_name"].ToString(),Building.Rows[i]["_id"].ToString()));
            }

            this.UpdatePanel2.Visible = false;
            gvGrpName.SelectedIndex = -1;
        }
        #endregion

        #region protected void NoEdit_Click(object sender, EventArgs e) //取消修改
        protected void NoEdit_Click(object sender, EventArgs e)
        {
            this.UpdatePanel2.Visible = false;
            this.Page.Response.Redirect("../Organization/DeviceManage.aspx");
        }
        #endregion

        #region protected void NoAdd_Click(object sender, EventArgs e) //取消新增
        protected void NoAdd_Click(object sender, EventArgs e)
        {
            this.UpdatePanel3.Visible = false;
            this.Page.Response.Redirect("../Organization/DeviceManage.aspx");
        }
        #endregion

        #region protected void gvGrpName_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) //修改設備
        protected void gvGrpName_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            this.UpdatePanel3.Visible = false;
            this.UpdatePanel2.Visible = true;
            
            Session["EditIndex"] = e.NewSelectedIndex;
            DeviceName.Text = this.gvGrpName.Rows[e.NewSelectedIndex].Cells[1].Text;
            DeviceDesc.Text = this.gvGrpName.Rows[e.NewSelectedIndex].Cells[2].Text;
            Session["DeviceID"] = this.gvGrpName.Rows[e.NewSelectedIndex].Cells[0].Text;

            DataTable dtDev =  new DBAccess().getDeviceTable("1");  //取得ALLDevice

            dtDev.DefaultView.RowFilter = "_id=" + this.gvGrpName.Rows[e.NewSelectedIndex].Cells[0].Text;
            dtDev = dtDev.DefaultView.ToTable();

            string strBlockID = dtDev.Rows[0]["_org_group_id"].ToString();

            //先查出大樓及樓層ID
            DataTable dtOrgGroup = new DBAccess().getOrgGroupTable("1");

            DataTable dtOrgGroupTemp = dtOrgGroup.Copy();
            dtOrgGroupTemp.DefaultView.RowFilter = "_id=" + strBlockID;
            dtOrgGroupTemp = dtOrgGroupTemp.DefaultView.ToTable();

            string strFloorID = dtOrgGroupTemp.Rows[0]["_parent_id"].ToString();

            dtOrgGroupTemp = dtOrgGroup.Copy();
            dtOrgGroupTemp.DefaultView.RowFilter = "_id=" + strFloorID;
            dtOrgGroupTemp = dtOrgGroupTemp.DefaultView.ToTable();

            string strBuildID = dtOrgGroupTemp.Rows[0]["_parent_id"].ToString();

            //重製ddl
            this.ddBuil.Items.Clear();
            this.ddBuil.Items.Add(new ListItem(string.Empty));
            this.ddfloor.Items.Clear();
            this.ddfloor.Items.Add(new ListItem(string.Empty));
            this.ddBlock.Items.Clear();
            this.ddBlock.Items.Add(new ListItem(string.Empty));

            //重製大樓ddl
            dtOrgGroupTemp = dtOrgGroup.Copy();
            dtOrgGroupTemp.DefaultView.RowFilter = "_level=1";
            dtOrgGroupTemp = dtOrgGroupTemp.DefaultView.ToTable();

            for (int i = 0; i < dtOrgGroupTemp.Rows.Count; i++)
            {
                this.ddBuil.Items.Add(new ListItem(dtOrgGroupTemp.Rows[i]["_name"].ToString(), dtOrgGroupTemp.Rows[i]["_id"].ToString()));
                if (dtOrgGroupTemp.Rows[i]["_id"].ToString() == strBuildID)
                    ddBuil.Items[ddBuil.Items.Count - 1].Selected = true;
            }

            //重製樓層ddl
            dtOrgGroupTemp = dtOrgGroup.Copy();
            dtOrgGroupTemp.DefaultView.RowFilter = "_level=2 and _parent_id='" + strBuildID + "'";
            dtOrgGroupTemp = dtOrgGroupTemp.DefaultView.ToTable();

            for (int i = 0; i < dtOrgGroupTemp.Rows.Count; i++)
            {
                this.ddfloor.Items.Add(new ListItem(dtOrgGroupTemp.Rows[i]["_name"].ToString(), dtOrgGroupTemp.Rows[i]["_id"].ToString()));
                if (dtOrgGroupTemp.Rows[i]["_id"].ToString() == strFloorID)
                    ddfloor.Items[ddfloor.Items.Count - 1].Selected = true;
            }

            //重製寑室ddl
            dtOrgGroupTemp = dtOrgGroup.Copy();
            dtOrgGroupTemp.DefaultView.RowFilter = "_level=3 and _parent_id='" + strFloorID + "'";
            dtOrgGroupTemp = dtOrgGroupTemp.DefaultView.ToTable();

            for (int i = 0; i < dtOrgGroupTemp.Rows.Count; i++)
            {
                this.ddBlock.Items.Add(new ListItem(dtOrgGroupTemp.Rows[i]["_name"].ToString(), dtOrgGroupTemp.Rows[i]["_id"].ToString()));
                if (dtOrgGroupTemp.Rows[i]["_id"].ToString() == strBlockID)
                    ddBlock.Items[ddBlock.Items.Count - 1].Selected = true;
            }

        }
        #endregion

        #region protected void gvGrpName_RowDeleting(object sender, GridViewDeleteEventArgs e) //刪除設備
        protected void gvGrpName_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //DataTable dtTemp = (DataTable)Session["dtDataSourceTemp"];
            //int devId = Convert.ToInt32(dtTemp.Rows[e.RowIndex]["_id"].ToString());
            //bool temp = new WebService().removeDevice1(devId);

            string deviceId = this.gvGrpName.Rows[e.RowIndex].Cells[0].Text;

            string strAccount = Session["LoginAccount"].ToString();
            string strIP = Session["LoginIP"].ToString();
            string strMemo = "ID：" + deviceId + "//名稱：" + this.gvGrpName.Rows[e.RowIndex].Cells[1].Text;

            bool blnRet = new DBAccess().deleteDeviceData(deviceId);

            if (blnRet == false)
            {
                new DBAccess().addEventLog(strAccount, strIP, 42, strMemo, "0");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('刪除設備資料有誤！')", true);
                return;
            }
            else
            {
                new DBAccess().addEventLog(strAccount, strIP, 42, strMemo, "1");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('刪除設備資料成功！')", true);
                //return;
            }
            //this.Page.Response.Redirect("../Organization/DeviceManage.aspx");
            this.UpdatePanel3.Visible = false;
            Panel4Clear();

            ddlControl_SelectedIndexChanged(this, null);
        }
        #endregion

        #region protected void YesEdit_Click(object sender, EventArgs e) //確定修改
        protected void YesEdit_Click(object sender, EventArgs e)
        {
            //DataTable dtTemp = (DataTable)Session["dtDataSourceTemp"];
            string devId = Session["DeviceID"].ToString();
            //Device modDev= new WebService().getDevice1(devId);
            //modDev.setName(DeviceName.Text);
            //modDev.setDescription(DeviceDesc.Text);
            //modDev.setEnabled(true);
            //Device temp = new WebService().modifyDevice1(modDev);

            string strAccount = Session["LoginAccount"].ToString();
            string strIP = Session["LoginIP"].ToString();
            string strMemo = "ID：" + devId + "//名稱：" + DeviceName.Text;

            bool blnRet = new DBAccess().updateDeviceData(DeviceName.Text, DeviceDesc.Text, ddBlock.SelectedValue, devId);
            if (blnRet == false)
            {
                new DBAccess().addEventLog(strAccount, strIP, 43, strMemo, "0", int.Parse(ddBlock.SelectedValue));
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改設備資料有誤！')", true);
                //return;
            }
            {
                new DBAccess().addEventLog(strAccount, strIP, 43, strMemo, "1", int.Parse(ddBlock.SelectedValue));
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改設備資料成功！')", true);
                //return;
            }
            this.UpdatePanel2.Visible = false;
            Panel3Clear();
            //this.Page.Response.Redirect("../Organization/DeviceManage.aspx");
            ddlControl_SelectedIndexChanged(this, null);
        }
        #endregion

        #region protected void YesAdd_Click(object sender, EventArgs e) //確定新增
        protected void YesAdd_Click(object sender, EventArgs e)
        {
            DataTable DTDev= (DataTable) Session["dtDataSourceTemp"];
            string refID = genRefId(DTDev);

            string path = "/1";  // customerRefId / buildingId / floorId / blockId
            path += "/" + ddlBuilding.SelectedValue + "/" + ddlFloor.SelectedValue + "/" + ddlBlock.SelectedValue;
            DataTable dtTemp = new DBAccess().getOrgGroupTable("1"); 

            //Device newDev=new Device();
            //newDev.setDatetime(new Date());
            //newDev.setEnabled(true);
            //newDev.setCustomerId(1);
            //newDev.setControlServerId(1);
            //newDev.setDeviceType(4);
            //newDev.setDeviceSubType(-1);
            //newDev.setName(tbDevName.Text);
            //newDev.setControllerId(Convert.ToInt32(ddlControl.SelectedValue));
            //newDev.setControlServerId(1);
            //newDev.setCustomerId(1);
            //newDev.setOrgGroupId(Convert.ToInt32(ddlBlock.SelectedValue));
            //newDev.setDescription(tbDes.Text);
            //newDev.setRefId(refID);

            //Device tmp = new WebService().createDevice1(newDev);
            //The OrgGroup path is incorrect. customerId: 1, parentId: 0, level: 1, refId: 1
            //Device tmp = new WebService().createDevice2(path, newDev);
            DataTable dtController = new DBAccess().getControllerTable("1");  //取得ALLOrgGroup
            dtController.DefaultView.RowFilter = "_id=" + ddlControl.SelectedValue;
            dtController = dtController.DefaultView.ToTable();

            string strAccount = Session["LoginAccount"].ToString();
            string strIP = Session["LoginIP"].ToString();
            string strMemo = "名稱：" + tbDevName.Text;

            bool blnRet = new DBAccess().addDeviceData(tbDevName.Text, tbDes.Text, dtController.Rows[0]["_ctrl_server_id"].ToString(), ddlControl.SelectedValue, ddlBlock.SelectedValue, refID);

            if (blnRet == false)
            {
                new DBAccess().addEventLog(strAccount, strIP, 41, strMemo, "0", int.Parse(ddlBlock.SelectedValue));
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增設備資料有誤！')", true);
                //return;
            }
            {
                new DBAccess().addEventLog(strAccount, strIP, 41, strMemo, "1", int.Parse(ddlBlock.SelectedValue));
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增設備資料成功！')", true);
                //return;
            }
            this.UpdatePanel3.Visible = false;
            Panel4Clear();
            //this.Page.Response.Redirect("../Organization/DeviceManage.aspx");
            ddlControl_SelectedIndexChanged(this, null);
        }
        #endregion

        #region private string genUniqueRefID(DataTable DT) //產生Ref_ID
        private string genRefId(DataTable DT)
        {
            string result = "";
            bool checkFlag = true;
            char[] key = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
                                      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
                                      'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
                                      'u', 'v', 'w', 'x', 'y', 'z',
                                      'A', 'B', 'C', 'D', 'E', 'f', 'G', 'H', 'I', 'J', 
                                      'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
                                      'U', 'V', 'W', 'X', 'Y', 'Z'};
            DataTable dtBuffer;

            System.Random rndGen = new System.Random();

            while (checkFlag == true)
            {
                result = "";

                for (int i = 0; i < 16; i++)
                {
                    int rnd = rndGen.Next(0, 62);
                    result += key[rnd];
                }

                DT.DefaultView.RowFilter = "_ref_id = '" + result + "'";
                dtBuffer = DT.DefaultView.ToTable();

                if (dtBuffer.Rows.Count == 0)
                    checkFlag = false;
            }

            return result;
        }
        #endregion

        protected void ddlBuilding_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlFloor.Items.Clear();
            this.ddlFloor.Items.Add(new ListItem(string.Empty));
            DataTable Floor = new DBAccess().getOrgGroupTable("1");  //取得ALLOrgGroup
            if (Floor == null)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('資料庫抓取資料有誤！')", true);
                return;
            }
            else
            {
                Floor.DefaultView.RowFilter = "_parent_id=" + this.ddlBuilding.SelectedValue.ToString();
                Floor = Floor.DefaultView.ToTable();
                for (int i = 0; i < Floor.Rows.Count; i++)
                    this.ddlFloor.Items.Add(new ListItem(Floor.Rows[i]["_name"].ToString(),Floor.Rows[i]["_id"].ToString()));
            }
        }

        protected void ddlFloor_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlBlock.Items.Clear();
            this.ddlBlock.Items.Add(new ListItem(string.Empty));
            DataTable Block = new DBAccess().getOrgGroupTable("1");  //取得ALLOrgGroup
            if (Block == null)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('資料庫抓取資料有誤！')", true);
                return;
            }
            else
            {
                Block.DefaultView.RowFilter = "_parent_id=" + this.ddlFloor.SelectedValue.ToString();
                Block = Block.DefaultView.ToTable();
                for (int i = 0; i < Block.Rows.Count; i++)
                    this.ddlBlock.Items.Add(new ListItem(Block.Rows[i]["_name"].ToString(),Block.Rows[i]["_id"].ToString()));
            }
            
        }

        protected void ddlControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlControl.SelectedIndex != 0)
            {

                this.btAddDevice.Visible = true;
                this.Panel1.Visible = true;
                Session["CountrolSelect"] = ddlControl.SelectedValue;
                DataTable dtDevice = new DBAccess().getDeviceTable("1");  //取得ALLDevice
                dtDevice.DefaultView.RowFilter = "_controller_id=" + ddlControl.SelectedValue.ToString();
                dtDevice = dtDevice.DefaultView.ToTable();
                if (dtDevice == null)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('資料庫抓取資料有誤！')", true);
                    return;
                }
                this.gvGrpName.DataSource = dtDevice;
                this.gvGrpName.DataBind();
            }
            else
            {
                this.btAddDevice.Visible = false;
                this.Panel1.Visible = false;
                
            }
            this.UpdatePanel3.Visible = false;
            this.UpdatePanel2.Visible = false;
            gvGrpName.SelectedIndex = -1;
        }

        protected void ddBuil_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddfloor.Items.Clear();
            this.ddfloor.Items.Add(new ListItem(string.Empty));
            DataTable Floor = new DBAccess().getOrgGroupTable("1");  //取得ALLOrgGroup
            if (Floor == null)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('資料庫抓取資料有誤！')", true);
                return;
            }
            else
            {
                Floor.DefaultView.RowFilter = "_parent_id=" + this.ddBuil.SelectedValue.ToString();
                Floor = Floor.DefaultView.ToTable();
                for (int i = 0; i < Floor.Rows.Count; i++)
                    this.ddfloor.Items.Add(new ListItem(Floor.Rows[i]["_name"].ToString(), Floor.Rows[i]["_id"].ToString()));
            }
        }

        protected void ddfloor_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddBlock.Items.Clear();
            this.ddBlock.Items.Add(new ListItem(string.Empty));
            DataTable Block = new DBAccess().getOrgGroupTable("1");  //取得ALLOrgGroup
            if (Block == null)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('資料庫抓取資料有誤！')", true);
                return;
            }
            else
            {
                Block.DefaultView.RowFilter = "_parent_id=" + this.ddfloor.SelectedValue.ToString();
                Block = Block.DefaultView.ToTable();
                for (int i = 0; i < Block.Rows.Count; i++)
                    this.ddBlock.Items.Add(new ListItem(Block.Rows[i]["_name"].ToString(), Block.Rows[i]["_id"].ToString()));
            }
        }

        #region private void Panel4Clear() //清除新增資料
        private void Panel4Clear()
        {
            tbDevName.Text = "";
            ddlBuilding.SelectedIndex = 0;
            ddlFloor.SelectedIndex = 0;
            ddlBlock.SelectedIndex = 0;
            tbDes.Text = "";

        }
        #endregion

        #region private void Panel3Clear() //清除修改資料
        private void Panel3Clear()
        {
            DeviceName.Text = "";
            ddBuil.SelectedIndex = 0;
            ddfloor.SelectedIndex = 0;
            ddBlock.SelectedIndex = 0;
            DeviceDesc.Text = "";

        }
        #endregion

        protected void gvGrpName_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;
        }
        
    }
}